﻿var newGame = {
    init : function init() {
        $('#start-game').on('click', newGame.startGame);

        $('#GameType').on('change', newGame.changeGameType);

        $('#player-properties').on('change', '.Player1Color', function () { newGame.changePlayerColor(this); });
    },

    startGame: function startGame() {
        var url = $('#game-properties').data('actionurl');
        var data = $('#game-properties').serializeArray();

        var player1Heuristics = [];
        $('#player1-properties input:checked').each(function () {
            player1Heuristics.push($(this).attr('name'));
        });

        var player2Heuristics = [];
        $('#player2-properties input:checked').each(function () {
            player2Heuristics.push($(this).attr('name'));
        });

        data.push({name:'Player1Heuristics', value:player1Heuristics});
        data.push({name:'Player2Heuristics', value:player2Heuristics});
        
        $.ajax({
            url: url,
            type: "POST",
            async: true,
            data: data,
            success: function (data) {
                //data - response from server
                window.location = data.url;
            }
        });
    },
    changeGameType: function changeGameType() {
        var val = $('#GameType').val();

        if (val === '0') {
            $('#player1-ai').addClass('conceal-sub-section');
            $('#player1-player').removeClass('conceal-sub-section');
        } else if (val === '1') {
            $('#player1-player').addClass('conceal-sub-section');
            $('#player1-ai').removeClass('conceal-sub-section');
        }
    },
    changePlayerColor: function changePlayerColor(element) {
        var val = $(element).val();

        if (val === '0') {
            $('.Player1Color').val('0');
            $('#Player2Color').text('Black');
        } else if (val === '1') {
            $('.Player1Color').val('1');
            $('#Player2Color').text('White');
        }
    }
}